package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.time.LocalTime;

import java.util.HashMap;

import java.util.Map;



@Service
public class WorkspeaceImpl implements WorkspaceService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 工作台今日数据查询
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        Map map = new HashMap();

        //所有订单
        map.put("begin", begin);
        map.put("end", end);
        Integer totalOrderCount = orderMapper.countByMap(map);

        //有效订单
        map.put("status", Orders.COMPLETED);
        Integer validOrdersCount = orderMapper.countByMap(map);

        //新用户数
        Integer newUser = userMapper.countByMap(map);

        //计算订单完成率
        //如果总订单数为0,则设置完成率为0,防止报错
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate =  validOrdersCount.doubleValue()  / totalOrderCount;
        }

        //营业额
        Double turnover = orderMapper.sumByMap(map);
            /*if (turnover != null) {//如果查出来没有订单,则直接赋值为0.0
                turnover = 0.0;
            }*/
        turnover = turnover == null ? 0.0 :turnover;

        //平均客单数
        double unitPrice = 0.0;
        if (validOrdersCount != 0 && totalOrderCount != 0){
            unitPrice = turnover / validOrdersCount;
        }


        return BusinessDataVO.builder()
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .turnover(turnover)
                .newUsers(newUser)
                .validOrderCount(validOrdersCount)
                .build();
    }






    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        LocalDateTime begin = LocalDateTime.now();
        Map map = new HashMap();
        map.put("begin",begin.with(LocalTime.MIN));
        map.put("end",begin.with(LocalTime.MAX));

        //全部订单
        Integer allOrders = orderMapper.countByMap(map);
        //已完成的订单
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);
        //已取消的订单
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);
        //待派送的订单
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);
        //待接单
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();

    }


    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        //已起售的菜品数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        //已停售的菜品数量
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        //已起售的菜品数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);
        //已停售的菜品数量
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
